<?php

namespace Plugins\Edge\Admin\Controller;

use App\Common\Exception\ApiMessageException;
use App\AbstractController;
use Plugins\Edge\Common\Validate\NewsValidate;
use Plugins\Edge\Admin\Service\NewsService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use App\Common\Util\Response;

/**
 * 新闻资讯控制器
 */
class NewsController extends AbstractController
{
    
    /**
     * 列表页
     *
     * @return string
     */
    public function lists(): string
    {
        return Response::render('plugins/Edge/News/lists');
    }

    /**
     * 列表数据
     *
     * @return \think\Response
     * @throws \Exception
     */
    public function listsData(): \think\Response
    {
        return Response::json()->success(NewsService::aop()->lists());
    }

    /**
     * 创建数据页面
     */
    public function create(): string
    {
        return Response::render('plugins/Edge/News/form');
    }

    /**
     * 创建数据
     *
     * @param NewsValidate $newsValidate
     *
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function store(NewsValidate $newsValidate): \think\Response
    {
        NewsService::aop()->store($newsValidate->post());

        return Response::json()->success();
    }

    /**
     * 更新数据页面
     *
     * @return string
     */
    public function edit(): string
    {
        return Response::render('plugins/Edge/News/form');
    }

    /**
     * 更新数据
     *
     * @param NewsValidate $newsValidate
     *
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function update(NewsValidate $newsValidate): \think\Response
    {
        NewsService::aop()->update($newsValidate->post());

        return Response::json()->success();
    }

    /**
     * 删除数据
     *
     * @param NewsValidate $newsValidate
     * @return \think\Response
     * @throws ApiMessageException
     */
    public function destroy(NewsValidate $newsValidate): \think\Response
    {
        NewsService::aop()->destroy($newsValidate->post('ids'));

        return Response::json()->success();
    }

    /**
     * 开关切换数值更新
     * @param NewsValidate $newsValidate
     *
     * @return \think\Response
     * @throws ApiMessageException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function switchToggle(NewsValidate $newsValidate): \think\Response
    {
        NewsService::aop()->toggle($newsValidate->post());

        return Response::json()->success();
    }
    
}
